﻿//-----------------------------------------------------------------------
// <copyright file="ConfigurationHelper.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation.
// All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
// </copyright>
// <author>Medha</author>
// <date>16-May-2011</date>
//-----------------------------------------------------------------------

namespace SmartTag.Configuration
{
    using System;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    using Microsoft.SharePoint;
    using SmartTag.Common.Utility;

    /// <summary>
    /// Helper class for Load/Save configuration
    /// </summary>
    public static class ConfigurationHelper
    {
        /// <summary>
        /// Loads the configuration.
        /// </summary>
        /// <param name="list">The Document Library.</param>
        /// <returns>Document Library Configuration</returns>
        public static DocumentLibraryConfiguration LoadConfiguration(SPList list)
        {
            DocumentLibraryConfiguration config = null;

            try
            {
                string listId = list.ID.ToString();
                XmlSerializer ser = new XmlSerializer(typeof(DocumentLibraryConfiguration));
                string strConfig = list.RootFolder.Properties["SmartTagConfiguration"] as string;

                if (string.IsNullOrEmpty(strConfig))
                {
                    config = new DocumentLibraryConfiguration();
                    config.ListId = listId;
                }
                else
                {
                    using (StringReader reader = new StringReader(strConfig))
                    {
                        config = ser.Deserialize(reader) as DocumentLibraryConfiguration;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error occured in LoadConfiguration", (int)TraceLogEventId.LoadConfiguration);
            }

            return config;
        }

        /// <summary>
        /// Saves the configuration.
        /// </summary>
        /// <param name="list">The Document Library.</param>
        /// <param name="config">The config.</param>
        public static void SaveConfiguration(SPList list, DocumentLibraryConfiguration config)
        {
            try
            {
                if (config == null)
                {
                    return;
                }

                XmlSerializer ser = new XmlSerializer(typeof(DocumentLibraryConfiguration));
                string strConfig = list.RootFolder.Properties["SmartTagConfiguration"] as string;
                StringBuilder sb = new StringBuilder();
                using (XmlWriter writer = XmlWriter.Create(sb))
                {
                    ser.Serialize(writer, config);
                }

                if (list.RootFolder.Properties.ContainsKey("SmartTagConfiguration"))
                {
                    list.RootFolder.Properties["SmartTagConfiguration"] = sb.ToString();
                }
                else
                {
                    list.RootFolder.Properties.Add("SmartTagConfiguration", sb.ToString());
                }

                list.RootFolder.Update();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error occured in SaveConfiguration", (int)TraceLogEventId.SaveConfiguration);
            }
        }

        /// <summary>
        /// Clears the configuration.
        /// </summary>
        /// <param name="list">The Document Library.</param>
        public static void ClearConfiguration(SPList list)
        {
            try
            {
                if (list.RootFolder.Properties.ContainsKey("SmartTagConfiguration"))
                {
                    list.RootFolder.Properties.Remove("SmartTagConfiguration");
                    list.RootFolder.Update();
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error occured in ClearConfiguration", (int)TraceLogEventId.ClearConfiguration);
            }
        }
    }
}
